//
//  CCYRecommendViewController.m
//  模拟-百思不得姐
//
//  Created by 程春玉 on 16/8/2.
//  Copyright © 2016年 chunyu. All rights reserved.
//

#import "CCYRecommendViewController.h"
#import "AFNetworking.h"
#import "SVProgressHUD.h"
#import "CCYRecommendModel.h"
#import "CCYChannelTableViewCell.h"
#import "CCYRecommendUserTableViewCell.h"
#import "CCYRecommendUserModel.h"


@interface CCYRecommendViewController () <UITableViewDelegate, UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UITableView *channelTab;
@property (weak, nonatomic) IBOutlet UITableView *userTab;
@property (nonatomic, strong) NSMutableArray *channelArr;

@end

@implementation CCYRecommendViewController

- (NSMutableArray *)channelArr
{
    if (!_channelArr) {
        _channelArr = [NSMutableArray array];
    }
    return _channelArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = XMGVColor;
    self.navigationItem.title = @"推荐关注";
    [SVProgressHUD show];
    [self createChannelTab];
    [self analyChannelData];
}

- (void)createChannelTab
{
    [_channelTab registerNib:[UINib nibWithNibName:@"CCYChannelTableViewCell" bundle:nil] forCellReuseIdentifier:@"channelCell"];
    [_userTab registerNib:[UINib nibWithNibName:NSStringFromClass([CCYRecommendUserTableViewCell class]) bundle:nil] forCellReuseIdentifier:@"userCell"];
    _channelTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _userTab.rowHeight = 70;
    _userTab.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    _userTab.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footAction)];
    _userTab.mj_footer.hidden = YES;
    _userTab.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headAction)];
    [_userTab.mj_header beginRefreshing];
}

- (void)footAction
{
    [self analyUserDataWithTypeModel];
}

- (void)headAction
{
    [_userTab.mj_header endRefreshing];
}

- (void)analyChannelData
{
    NSDictionary *dic = @{@"a" : @"category", @"c" : @"subscribe"};
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    [[AFHTTPSessionManager manager] GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dict in responseObject[@"list"]) {
            CCYRecommendModel *model = [[CCYRecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            model.currPage = 1;
            [self.channelArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.channelTab reloadData];
            [self.channelTab selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:(UITableViewScrollPositionTop)];
            [self analyUserDataWithTypeModel];
            [SVProgressHUD dismiss];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"加载失败，请检查网络"];
    }];
}

- (void)analyUserDataWithTypeModel
{
    CCYRecommendModel *typeModel = _channelArr[[_channelTab indexPathForSelectedRow].row];
    NSDictionary *dic = @{@"a" : @"list", @"c" : @"subscribe", @"category_id" : @(typeModel.id), @"page" : @(typeModel.currPage++)};
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    [[AFHTTPSessionManager manager] GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        for (NSDictionary *dict in responseObject[@"list"]) {
            CCYRecommendUserModel *model = [[CCYRecommendUserModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [typeModel.users addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.userTab reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"推荐关注加载失败，请检查网络"];
    }];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == _channelTab) {
        return self.channelArr.count;
    } else {
        if (_channelArr.count != 0) {
            _userTab.mj_footer.hidden = NO;
            [self judgeData];
            return [_channelArr[[_channelTab indexPathForSelectedRow].row] users].count;
        }
        _userTab.mj_footer.hidden = YES;
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _channelTab) {
        CCYChannelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"channelCell" forIndexPath:indexPath];
        [cell setMessageWithModel:_channelArr[indexPath.row]];
        return cell;
    } else {
        CCYRecommendUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"userCell" forIndexPath:indexPath];
        if ([[_channelArr[[_channelTab indexPathForSelectedRow].row] users] count] != 0) {
            [cell setMessageWithUserModel:[_channelArr[[_channelTab indexPathForSelectedRow].row] users][indexPath.row]];
        }
        return cell;
    }
    
}

- (void)judgeData
{
    CCYRecommendModel *typeModel = _channelArr[[_channelTab indexPathForSelectedRow].row];
    if (typeModel.users.count == typeModel.count) {
        [self.userTab.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.userTab.mj_footer endRefreshing];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _channelTab) {
        if ([_channelArr[indexPath.row] users].count == 0) {
            [self analyUserDataWithTypeModel];
            [_userTab.mj_header beginRefreshing];
        } else {
            [self.userTab reloadData];
            [self.userTab selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:(UITableViewScrollPositionTop)];
        }
    }
}





@end
